package com.map.station;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class MapsActivity extends MapActivity {

	private MapView mapView;
	private MapController mapControl;
	private static boolean satellite, streetView, traffic;
	
	private static Intent intent;
	private static Bundle bundle = new Bundle();
	
	//class MapOverLay extends com.google.android.maps.Overlay {
		/*@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			if(event.getAction() == 1) {
				GeoPoint p = mapView.getProjection().fromPixels(
						(int)event.getX(), (int)event.getY());
				Toast.makeText(getBaseContext(), "Location :"+p.getLongitudeE6()/1E6
						+","+p.getLatitudeE6()/1E6, Toast.LENGTH_SHORT).show();
			}
			return false;
		}*/
		
		/*@Override
		public boolean onTap(GeoPoint point, MapView mapView) 
		{
		   Context contexto = mapView.getContext();
		   String msg = "Lat: " + point.getLatitudeE6()/1E6 + " - " + 
		                "Lon: " + point.getLongitudeE6()/1E6;

		   Toast toast = Toast.makeText(contexto, msg, Toast.LENGTH_SHORT);
		   toast.show();

		  return true;
		 }*/
	//}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapview);
		
		satellite = OptionPreference.setSatellite(getBaseContext());
		streetView = OptionPreference.setStreetView(getBaseContext());
		traffic = OptionPreference.setTraffic(getBaseContext());
		
		initMapView();
		initMyLocation();
		drawStation();
		
		// return longitude and latitude 
		//MapOverLay mapOverLay = new MapOverLay();
		//List<Overlay> listOfOverlays = mapView.getOverlays();
		//listOfOverlays.add(mapOverLay);
	}
	
	// set Satellite, StreetView, Traffic
	private void initMapView() {
		mapView = (MapView)findViewById(R.id.mapView);
		mapControl = mapView.getController();
		mapView.setSatellite(satellite);
		mapView.setStreetView(streetView);
		mapView.setTraffic(traffic);
		mapView.setBuiltInZoomControls(true);
	}
	
	// get and mark point to current location
	private void initMyLocation() {
		final MyLocationOverlay overlay = new MyLocationOverlay(this, mapView);
		overlay.enableMyLocation();
		overlay.runOnFirstFix(new Runnable() {
			
			@Override
			public void run() {
				mapControl.setZoom(14);
				mapControl.animateTo(overlay.getMyLocation());
			}
		});
		mapView.getOverlays().add(overlay);
	}
	
	// draw image station on google map
	public void drawStation() {
		
		// set image
		Drawable shell = getResources().getDrawable(R.drawable.shell);
		Drawable esso = getResources().getDrawable(R.drawable.esso);
		Drawable caltex = getResources().getDrawable(R.drawable.caltex);
		Drawable ptt = getResources().getDrawable(R.drawable.ptt);
		Drawable bcp = getResources().getDrawable(R.drawable.bcp);
		
		shell.setBounds(0, 0, shell.getIntrinsicWidth(), shell.getIntrinsicHeight());
		esso.setBounds(0, 0, esso.getIntrinsicWidth(), esso.getIntrinsicHeight());
		caltex.setBounds(0, 0, caltex.getIntrinsicWidth(), caltex.getIntrinsicHeight());
		ptt.setBounds(0, 0, ptt.getIntrinsicWidth(), ptt.getIntrinsicHeight());
		bcp.setBounds(0, 0, bcp.getIntrinsicWidth(), bcp.getIntrinsicHeight());
		
		Shell Sshell = new Shell(shell);
		Esso Eesso = new Esso(esso);
		Caltex Ccaltex = new Caltex(caltex);
		Ptt Pptt = new Ptt(ptt);
		Bcp Bbcp = new Bcp(bcp);
		
		// add overlay
		mapView.getOverlays().add(Sshell);
		mapView.getOverlays().add(Eesso);
		mapView.getOverlays().add(Ccaltex);
		mapView.getOverlays().add(Pptt);
		mapView.getOverlays().add(Bbcp);
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	//-----------------------------------------------------------------------------//
	//Esso Shell all about Shell Marker
	private class Shell extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String name = "shell";
		
		private Shell(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint shell1 = new GeoPoint((int)(7.88599*1000000)
					, (int)(98.38885*1000000));
			GeoPoint shell2 = new GeoPoint((int)(7.88291*1000000)
					, (int)(98.39931*1000000));
			GeoPoint shell3 = new GeoPoint((int)(7.868459*1000000)
					, (int)(98.356324*1000000));
			GeoPoint shell4 = new GeoPoint((int)(7.90827*1000000)
					, (int)(98.356324*1000000));

			location.add(new OverlayItem(shell1, "shell1", "shell1"));
			location.add(new OverlayItem(shell2, "shell2", "shell2"));
			location.add(new OverlayItem(shell3, "shell3", "shell3"));
			location.add(new OverlayItem(shell4, "shell4", "shell4"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			//Toast.makeText(getBaseContext(), "Index : "+index, Toast.LENGTH_LONG).show();
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putInt("indexKeyword", index);
			bundle.putString("stationKeyword", name);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}
	//-----------------------------------------------------------------------------//
	//Esso class all about Esso Marker
	private class Esso extends ItemizedOverlay {

		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String name = "esso";
		
		private Esso(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint esso1 = new GeoPoint((int)(7.90199*1000000)
					, (int)(98.3687*1000000));
			GeoPoint esso2 = new GeoPoint((int)(7.89017*1000000)
					, (int)(98.39019*1000000));
			GeoPoint esso3 = new GeoPoint((int)(7.8789*1000000)
					, (int)(98.38124*1000000));
			GeoPoint esso4 = new GeoPoint((int)(7.89796*1000000)
					, (int)(98.38906*1000000));

			location.add(new OverlayItem(esso1, "esso1", "esso1"));
			location.add(new OverlayItem(esso2, "esso2", "esso2"));
			location.add(new OverlayItem(esso3, "esso3", "esso3"));
			location.add(new OverlayItem(esso4, "esso4", "esso4"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			//Toast.makeText(getBaseContext(), "Index : "+index, Toast.LENGTH_LONG).show();
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putInt("indexKeyword", index);
			bundle.putString("stationKeyword", name);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}
	//-----------------------------------------------------------------------------//
	//Caltex class all about Caltex Marker
	private class Caltex extends ItemizedOverlay {

		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		private Drawable marker;
		private String name = "caltex";
		
		private Caltex(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint caltex1 = new GeoPoint((int)(8.02331*1000000)
					, (int)(98.333945*1000000));

			location.add(new OverlayItem(caltex1, "caltex1", "caltex1"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}

		@Override
		public int size() {
			return location.size();
		}	
		
		@Override
		protected boolean onTap(int index) {
			//Toast.makeText(getBaseContext(), "Index : "+index, Toast.LENGTH_LONG).show();
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putInt("indexKeyword", index);
			bundle.putString("stationKeyword", name);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}
	//-----------------------------------------------------------------------------//
	//Caltex Ptt all about Ptt Marker
	private class Ptt extends ItemizedOverlay {
		
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		
		private Drawable marker;
		private String name = "ptt";
		
		private Ptt(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint ptt1 = new GeoPoint((int)(7.91028*1000000)
					, (int)(98.39176*1000000));
			GeoPoint ptt2 = new GeoPoint((int)(7.87872*1000000)
					, (int)(98.38541*1000000));
			GeoPoint ptt3 = new GeoPoint((int)(7.842094*1000000)
					, (int)(98.334413*1000000));
			
			location.add(new OverlayItem(ptt1, "ptt1", "ptt1"));
			location.add(new OverlayItem(ptt2, "ptt2", "ptt2"));
			location.add(new OverlayItem(ptt3, "ptt3", "ptt3"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}
		
		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			//Toast.makeText(getBaseContext(), "Index : "+index, Toast.LENGTH_LONG).show();
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putInt("indexKeyword", index);
			bundle.putString("stationKeyword", name);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}
	//-----------------------------------------------------------------------------//
	//Caltex Bcp all about Bcp Marker
	private class Bcp extends ItemizedOverlay {
		private List<OverlayItem> location = new ArrayList<OverlayItem>();
		
		private Drawable marker;
		private String name = "bcp";
		
		public Bcp(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			GeoPoint bcp1 = new GeoPoint((int)(7.807598*1000000)
					, (int)(98.336991*1000000));
			
			location.add(new OverlayItem(bcp1, "bcp1", "bcp1"));
			populate();
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			boundCenterBottom(marker);
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			return location.get(i);
		}
		
		@Override
		public int size() {
			return location.size();
		}
		
		@Override
		protected boolean onTap(int index) {
			//Toast.makeText(getBaseContext(), "Index : "+index, Toast.LENGTH_LONG).show();
			intent = new Intent(getBaseContext(), DetailActivity.class);
			bundle.putInt("indexKeyword", index);
			bundle.putString("stationKeyword", name);
			intent.putExtras(bundle);
			startActivity(intent);
			return false;
		}
	}
}
